﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：销户
//********************************************************************************//

using System;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;
using Web.UC;

namespace Web.Pages.YongShui.YongHuGL
{
    public partial class XiaoHu : BasePage, IConfirmDo
    {
        #region 属性

        /// <summary>
        ///     当前操作项ID
        /// </summary>
        private int YONGHUXXID
        {
            get { return Convert.ToInt32(ViewState["_YongHuXXID"]); }
            set { ViewState["_YongHuXXID"] = value; }
        }

        /// <summary>
        ///     记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        /// <summary>
        ///     记录列表二的查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get { return ViewState["dtSource2"] as DataTable; }
            set { ViewState["dtSource2"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     用户信息对象
        /// </summary>
        private readonly YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
                LoadData();
            }
            UCConfirm1.ConfirmDoPage = this;
        }

        /// <summary>
        ///     绑定空表头
        /// </summary>
        /// <param name="sender"></param>
        protected override void Render(HtmlTextWriter sender)
        {
            var dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic("1=0", "", null);
            base.Render(sender);
        }

        /// <summary>
        ///     查询操作
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Search;
            //根据来涵编号查询出用户号,暂写固定
            var yonghuH = txtYongHuH.Text;
            ViewState["YongHuh"] = yonghuH;
            //查询下一月用户信息
            dtSource = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(
                string.Format("S_YONGHUH='{0}' AND I_LEIXING=1 AND I_JLZT=0", yonghuH), null);
            if (dtSource.Rows.Count != 0)
            {
                var userRow = dtSource.Rows[0];
                txtUserName.Text = userRow["S_YONGHUM"].ToString();
                txtAddress.Text = userRow["S_DIZHI"].ToString();
                txtContactor.Text = userRow["S_LIANXIR"].ToString();
                txtDepartment.Text = userRow["S_LIANXIBM"].ToString();
                txtPhone.Text = userRow["S_LIANXIDH"].ToString();
                txtPostCode.Text = userRow["S_YOUBIAN"].ToString();
                //加载水表信息
                dtSource2 = _spHelper.P_SEL_YS_SHUIBIAOBYYH(yonghuH, (int) eI_LEIXING.NextMonth).Tables[0];
                gvList2.DataSource = dtSource2;
                gvList2.DataBind();
            }
            else
            {
                PageTools.AlertMsg(this,"用户不存在");
                ClearDisplay();
            }
            upList.Update();
        }

        /// <summary>
        ///     销户操作
        /// </summary>
        protected void btnXiaoHu_Click(object sender, EventArgs e)
        {
            //判断是否可以销户
            //无用户数据可销
            if (dtSource == null)
                return;
            //用户有水表使用
            if (dtSource2 != null && dtSource2.Rows.Count > 0)
            {
                UCAlert1.Msg = "当前用户还有水表,不允许销户.";
                UCAlert1.Show();
                return;
            }
            //执行销户          
            _YS_YG_YONGHUXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            _YS_YG_YONGHUXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _YS_YG_YONGHUXX.I_YONGHUZT = WordsStruct.I_YONGHUZT.销户;
            _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-销户", OperateState.Delete,
                _YS_YG_YONGHUXX, null);
            var helper = new SqlHelper();
            if (helper.DeleteUser(ViewState["YongHuh"].ToString()))
            {
                PageTools.AlertMsg(this,"销户成功");
                ClearDisplay();
            }
            else
            {
                PageTools.AlertMsg(this,"销户失败");
            }
        }

        #endregion

        #region 方法

        private void ClearDisplay()
        {
            txtUserName.Text = "";
            txtAddress.Text = "";
            txtContactor.Text = "";
            txtDepartment.Text = "";
            txtPhone.Text = "";
            txtPostCode.Text = "";
            dtSource2 = null;
            gvList2.DataSource = null;
            gvList2.DataBind();
        }

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
        }

        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    break;
                case GridViewStatus.Search:
                    break;
                case GridViewStatus.Update:
                    break;
                case GridViewStatus.Add:
                    break;
            }
        }

        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
        }

        /// <summary>
        ///     保存数据
        /// </summary>
        private void SaveData()
        {
        }

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData()
        {
        }

        /// <summary>
        ///     实现confirm接口的确定按钮方法
        /// </summary>
        public void ConfirmDo()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}